<script setup lang="ts">
import { ref, onMounted, reactive, watch } from "vue";
import { useUserStore } from "@/store/user";
import { ElMessage } from "element-plus";

const userStore = useUserStore();
const loading = ref(false);
const userInfo = reactive({
  username: userStore.userInfo?.username || "",
  email: userStore.userInfo?.email || "",
  phone: userStore.userInfo?.phone || "",
});

// 更新个人信息
const updateProfile = async () => {
  loading.value = true;
  // 这里应该调用API更新用户信息
  const res = await userStore.saveUserInfoAction({ ...userInfo });
  if (res) {
    ElMessage.success("个人信息更新成功");
    await userStore.getUserInfoAction();
    Object.assign(userInfo, {
      username: userStore.userInfo?.username || "",
      email: userStore.userInfo?.email || "",
      phone: userStore.userInfo?.phone || "",
    });
  }
  loading.value = false;
};
</script>
<template>
  <el-card class="mt-4">
    <template #header>
      <div class="card-header">
        <span>个人信息</span>
      </div>
    </template>

    <el-form :model="userInfo" label-width="120px">
      <el-form-item label="用户名">
        <el-input v-model="userInfo.username" disabled />
      </el-form-item>

      <el-form-item label="邮箱">
        <el-input v-model="userInfo.email" />
      </el-form-item>

      <el-form-item label="手机号">
        <el-input v-model="userInfo.phone" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" :loading="loading" @click="updateProfile"
          >更新信息</el-button
        >
      </el-form-item>
    </el-form>
  </el-card>
</template>
